home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cocktail / ell.lha / ell / m2c / Derivable.c < prev    next >
C/C++ Source or Header  |  1992-08-18  |  3KB  |  157 lines

  1. #include "SYSTEM_.h"
  2.  
  3. #ifndef DEFINITION_General
  4. #include "General.h"
  5. #endif
  6.  
  7. #ifndef DEFINITION_Idents
  8. #include "Idents.h"
  9. #endif
  10.  
  11. #ifndef DEFINITION_Sets
  12. #include "Sets.h"
  13. #endif
  14.  
  15. #ifndef DEFINITION_Table
  16. #include "Table.h"
  17. #endif
  18.  
  19. #ifndef DEFINITION_Tree
  20. #include "Tree.h"
  21. #endif
  22.  
  23. #ifndef DEFINITION_Types
  24. #include "Types.h"
  25. #endif
  26.  
  27. #ifndef DEFINITION_Derivable
  28. #include "Derivable.h"
  29. #endif
  30.  
  31.  
  32. static Sets_tSet vDerivables;
  33. static INTEGER infinite;
  34. static void YetLength ARGS((Tree_tTree t));
  35.  
  36.  
  37. void Derivable_TestDerivable
  38. # ifdef __STDC__
  39. ()
  40. # else
  41. ()
  42. # endif
  43. {
  44.   Sets_tSet N;
  45.   Idents_tIdent nt;
  46.   Tree_tTree t;
  47.   BOOLEAN success;
  48.  
  49.   Sets_MakeSet(&N, (LONGCARD)Idents_MaxIdent());
  50.   do {
  51.     Types_Nonterminals(&N);
  52.     success = FALSE;
  53.     while (!Sets_IsEmpty(N)) {
  54.       nt = Sets_Extract(&N);
  55.       t = Table_Expr(nt);
  56.       YetLength(t);
  57.       if (t->U_1.V_32.expr.length < Table_Length(nt)) {
  58.         success = TRUE;
  59.         Table_SetLength(nt, t->U_1.V_32.expr.length);
  60.       }
  61.     }
  62.   } while (!!success);
  63.   Sets_ReleaseSet(&N);
  64. }
  65.  
  66. static void YetLength
  67. # ifdef __STDC__
  68. (Tree_tTree t)
  69. # else
  70. (t)
  71. Tree_tTree t;
  72. # endif
  73. {
  74.   switch (t->U_1.V_1.Kind) {
  75.   case Tree_Option:;
  76.     YetLength(t->U_1.V_33.Option.expr);
  77.     t->U_1.V_33.Option.length = 0;
  78.     break;
  79.   case Tree_Times:;
  80.     YetLength(t->U_1.V_34.Times.expr);
  81.     t->U_1.V_34.Times.length = 0;
  82.     break;
  83.   case Tree_Plus:;
  84.     YetLength(t->U_1.V_35.Plus.expr);
  85.     t->U_1.V_35.Plus.length = t->U_1.V_35.Plus.expr->U_1.V_32.expr.length;
  86.     break;
  87.   case Tree_List:;
  88.     YetLength(t->U_1.V_36.List.body);
  89.     YetLength(t->U_1.V_36.List.sep);
  90.     t->U_1.V_36.List.length = t->U_1.V_36.List.body->U_1.V_32.expr.length;
  91.     break;
  92.   case Tree_Action:;
  93.     t->U_1.V_37.Action.length = 0;
  94.     break;
  95.   case Tree_Leaf:;
  96.     t->U_1.V_38.Leaf.length = Table_Length(t->U_1.V_38.Leaf.id->U_1.V_23.Id.ident);
  97.     break;
  98.   case Tree_Alternative0:;
  99.     t->U_1.V_40.Alternative0.length = infinite;
  100.     break;
  101.   case Tree_Alternative:;
  102.     YetLength(t->U_1.V_41.Alternative.expr);
  103.     YetLength(t->U_1.V_41.Alternative.alternative);
  104.     t->U_1.V_41.Alternative.length = General_Min(t->U_1.V_41.Alternative.expr->U_1.V_32.expr.length, t->U_1.V_41.Alternative.alternative->U_1.V_32.expr.length);
  105.     break;
  106.   case Tree_Sequence0:;
  107.     t->U_1.V_43.Sequence0.length = 0;
  108.     break;
  109.   case Tree_Sequence:;
  110.     YetLength(t->U_1.V_44.Sequence.expr);
  111.     YetLength(t->U_1.V_44.Sequence.sequence);
  112.     t->U_1.V_44.Sequence.length = General_Min(t->U_1.V_44.Sequence.sequence->U_1.V_32.expr.length + t->U_1.V_44.Sequence.expr->U_1.V_32.expr.length, infinite);
  113.     break;
  114.   }
  115. }
  116.  
  117. BOOLEAN Derivable_IsDerivable
  118. # ifdef __STDC__
  119. (Idents_tIdent Nonterm)
  120. # else
  121. (Nonterm)
  122. Idents_tIdent Nonterm;
  123. # endif
  124. {
  125.   return Table_Length(Nonterm) < infinite;
  126. }
  127.  
  128. BOOLEAN Derivable_IsNullable
  129. # ifdef __STDC__
  130. (Idents_tIdent Nonterm)
  131. # else
  132. (Nonterm)
  133. Idents_tIdent Nonterm;
  134. # endif
  135. {
  136.   return Table_Length(Nonterm) == 0;
  137. }
  138.  
  139. void BEGIN_Derivable()
  140. {
  141.   static BOOLEAN has_been_called = FALSE;
  142.  
  143.   if (!has_been_called) {
  144.     has_been_called = TRUE;
  145.  
  146.     BEGIN_Idents();
  147.     BEGIN_General();
  148.     BEGIN_Idents();
  149.     BEGIN_Sets();
  150.     BEGIN_Table();
  151.     BEGIN_Tree();
  152.     BEGIN_Types();
  153.  
  154.     infinite = MAX_LONGINT / 2;
  155.   }
  156. }
  157.